import sys
from datetime import datetime as dt

def log_error_obj (error_obj):
    print("[{dt}] {0.__class__.__name__}: {0}".format(error_obj, dt=dt.now()), file=sys.stderr)

def fgetb (name):
    try:
        with open(name, "rb") as f:
            return f.read()
    except IOError as er:
        log_error_obj(er)
        return b""

def fsetb (name, value):
    try:
        with open(name, "wb") as f:
            f.write(value)
        return True
    except IOError as er:
        log_error_obj(er)
        return False

def fget (name):
    return fgetb(name).decode()

def fset (name, value):
    return fsetb(name, value.encode())

class Anon(dict):
    def __init__ (self, *args, **kwargs):
        super().__init__(*args, **kwargs)
    
    def __getattr__ (self, attr):
        try:
            return self[attr]
        except KeyError:
            raise AttributeError("Attribute {} does not exist".format(attr))

def anonify (obj):
    if isinstance(obj, dict):
        return Anon({k: anonify(v) for k, v in obj.items()})
    elif isinstance(obj, list):
        return [anonify(v) for v in obj]
    else:
        return obj
